﻿<!DOCTYPE html>
<html>
<head>
    <script src="/js/header.js"></script>
</head>
<body class="bg_white">
    <section class="container-fluid" style="background-color: white;">
        <form class="form-horizontal m15_t" id="form">
            <div class="tab-content">
                <div class="tab-pane fade in active" id="divCompany">
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>公司名称：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="companyName" maxlength="40" placeholder="公司名称长度必须介于 3 和 40 之间" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">联络负责人：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="companyBossName" maxlength="40" placeholder="长度不能超过40位" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">联络电话：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="companyTel" maxlength="40" placeholder="请正确输入电话格式" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">联络地址：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="companyAddress" maxlength="40" placeholder="长度不能超过40位" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">公司英文名称：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="companyNameEn" maxlength="40" placeholder="长度不能超过40位" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>收款费率(%)：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="depositRate" maxlength="5" value="0" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>付款费率(%)：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="payRate" maxlength="5" value="0" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>中转费率(%)：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="transportRate" maxlength="5" value="0" />
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </section>
    <script type="text/javascript">
        var validator = null;
        var isCommitted = false;
        $(function () {
            var id = FlashPay.Util.GetQueryString("id");
            if (id != null && id != undefined) {
                showCompanyInfo(id);
            }
            validator = FlashPay.Util.Validator({
                formObj: $("#form"),
                rules: {
                    companyName: {
                        required: true,
                        rangelength: [3, 40],
                        //specialChar: true
                    },
                    companyBossName: {
                        specialChar: true
                    },
                    companyTel: {
                        isPhone: true
                    }
                    , companyNameEn: {
                        specialChar: true
                    }
                    , depositRate: {
                        required: true,
                        money: true
                    }
                    , payRate: {
                        required: true,
                        money: true
                    }
                    , transportRate: {
                        required: true,
                        money: true
                    }
                },
                messages: {
                    companyName: {
                        required: "公司名称不能为空",
                        rangelength: "公司名称长度必须介于 3 和 40 之间"
                        //, specialChar: "公司名称不能带有特殊字符"
                    },
                    companyBossName: {
                        specialChar: "联络负责人不能带有特殊字符",
                    },
                    companyTel: {
                        isPhone: "请正确填写您的联系电话",
                    },
                    companyNameEn: {
                        specialChar: "公司英文名称不能带有特殊字符",
                    },
                    depositRate: {
                        required: "存款费率不能为空",
                        money: "必须为大于0的金额，最多保留两位小数"
                    },
                    payRate: {
                        required: "付款费率不能为空",
                        money: "必须为大于0的金额，最多保留两位小数"
                    },
                    transportRate: {
                        required: "中转费率不能为空",
                        money: "必须为大于0的金额，最多保留两位小数"
                    }
                }
            });
        });

        //显示值
        function showCompanyInfo(id) {
            var cover = FlashPay.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });
            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Company/GetDetail",
                data: JSON.stringify({ CompanyID: id }),
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (data.totalCount > 0) {
                        var infos = data.result[0];
                        FlashPay.Util.SetInputVal("companyName", infos.companyName);
                        FlashPay.Util.SetInputVal("companyBossName", infos.companyBossName);
                        FlashPay.Util.SetInputVal("companyTel", infos.companyTel);
                        FlashPay.Util.SetInputVal("companyAddress", infos.companyAddress);
                        FlashPay.Util.SetInputVal("companyNameEn", infos.companyNameEN);
                        FlashPay.Util.SetInputVal("depositRate", infos.depositRate*100);
                        FlashPay.Util.SetInputVal("payRate", infos.payRate * 100);
                        FlashPay.Util.SetInputVal("transportRate", infos.transportRate * 100);
                    }
                    cover.Remove();
                },
                error: function (json) {
                    cover.Remove();
                }
            });
        }

        function save() {
            if (!validator.form()) {
                return false;
            }
            var CompanyViewModel = {};
            CompanyViewModel.CompanyName = $("input[name='companyName']").val();
            CompanyViewModel.CompanyBossName = $('input[name="companyBossName"]').val();
            CompanyViewModel.CompanyTel = $("input[name='companyTel']").val();
            CompanyViewModel.CompanyAddress = $("input[name='companyAddress']").val();
            CompanyViewModel.CompanyNameEn = $('input[name="companyNameEn"]').val();
            CompanyViewModel.DepositRate = $("input[name='depositRate']").val();
            CompanyViewModel.PayRate = $("input[name='payRate']").val();
            CompanyViewModel.TransportRate = $("input[name='transportRate']").val();

            var url = "/Company/Add";
            var id = FlashPay.Util.GetQueryString("id");
            if (id != null && id != undefined) {
                url = "/Company/Edit";
                CompanyViewModel.CompanyID = id;
            } 
            if ($("body").data("confirmReceive") == "lock") {
                FlashPay.UI.Tip_short_danger("操作过快");
                return false;
            } else {
                $("body").data("confirmReceive", "lock");
            }
            FlashPay.Util.Ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(CompanyViewModel),
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    $("body").data("confirmReceive", "unlock");
                    if (!data.success) {
                        if (data.errorCode == "100" || data.errorCode == "500") {
                            return;
                        } else {
                            FlashPay.UI.Tip_short_warning(data.errorMessage == null ? "公司名系统已重复，如果问题，请联系管理员！" : data.errorMessage);
                            return;
                        }
                    } else {
                        FlashPay.UI.DialogOpener().Tip_success(data.successMessage == null ? "操作成功！" : data.successMessage);
                        FlashPay.UI.DialogOpener().doSearch();
                        FlashPay.UI.CloseDialog();
                    }
                }
            });
        }
    </script>
</body>
</html>